
export const success = (msg) => {
    showMessage("success", msg);
};


export  let userConfirmed=false;



export const Confirm = (msg) => {

    // 确保在使用之前加载必要的 layui 模块
    layui.use(['layer', 'util'], function() {
        let layer = layui.layer;
        let util = layui.util;
        layer.confirm("确定要"+msg+"操作", {icon: 3}, function(index){
            userConfirmed=true;
            layer.close(index);
        }, function(index){
            layer.msg("取消成功", {icon: 1});
            layer.close(index);
        });

    });
};


export const showMessage = (type, msg) => {
    // 确保在使用之前加载必要的 layui 模块
    layui.use(['layer', 'util'], function() {
        let layer = layui.layer;
        let util = layui.util;

        switch (type) {
            case 'success':
                msg = msg === " " ? "操作成功" : msg;
                layer.msg("操作成功", {icon: 1});
                break;
        }
    });
};





layui.use(function(){
    var layer = layui.layer;
    var util = layui.util;
    // 事件
    util.on('lay-on', {
        "test-alert": function(){
            // 示范对话框所有内置图标
            var icon = 0;
            (function changeIcon(){
                layer.alert('点击确定，继续查看图标', {
                    icon: icon,
                    shadeClose: true,
                    title: 'icon: '+ icon
                }, ++icon > 6 ? function(){
                    layer.msg('内置图标演示完毕', {icon: 1});
                } : changeIcon);
            }());
        },
        "test-confirm": function(){
            layer.confirm('一个询问框的示例？', {icon: 3}, function(){
                layer.msg('点击确定的回调', {icon: 1});
            }, function(){
                layer.msg('点击取消的回调');
            });
        },
        "test-msg-dark": function(){
            layer.msg('深色提示框的示例');
        },
        "test-msg-light": function(){
            layer.msg('浅色提示框的示例', {icon: 0}, function(){
                // layer.msg('提示框关闭后的回调');
            });
        },
        "test-alert-btn": function(){
            layer.alert('自定义按钮', {
                btn: ['按钮一', '按钮二', '按钮三'],
                btnAlign: 'c', // 按钮居中显示
                btn1: function(){
                    layer.msg('按钮一的回调');
                },
                btn2: function(){
                    layer.msg('按钮二的回调');
                },
                btn3: function(){
                    layer.msg('按钮三的回调');
                }
            });
        },
        "test-count-down": function(){
            layer.alert('在标题栏显示自动关闭倒计秒数', {
                time: 5*1000,
                success: function(layero, index){
                    var timeNum = this.time/1000, setText = function(start){
                        layer.title('<span class="layui-font-red">'+ (start ? timeNum : --timeNum) + '</span> 秒后自动关闭', index);
                    };
                    setText(!0);
                    this.timer = setInterval(setText, 1000);
                    if(timeNum <= 0) clearInterval(this.timer);
                },
                end: function(){
                    clearInterval(this.timer);
                }
            });
        }
    })
});
